<template>
	<view class="main">
		<TopBar :indexflag="true" :combakflag="true" :lang="false" :bottom="true" :title="$t('EmployeeAdmin')"
			:barstyle="colortopic()" Color="#fff"></TopBar>
		<view class="shop-list" :dir="i18nLocale=='ug'?'rtl':''">
			<view class="shop-item" v-for="(item,index) in employeeList" @click="EditEmployee(item)">
				<view class="left">
					<view class="shop-content">
						<view class="title">{{item.name}}</view>
						<view class="mobile">{{item.mobile}}</view>
					</view>
				</view>
				<view class="right">
					<view class="button edit">
						<u-icon name="edit-pen" color="#f9ae3d" size="22"></u-icon>
						<text>{{$t('Edit')}}</text>
					</view>
					<view class="button delet" @click.stop="deletEmployees(item.id)">
						<u-icon name="trash" color="#cc0000" size="22"></u-icon>
						<text class="" style="#cc0000">{{ $t('Delete')}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="footer-button">
			<view class="button-item" @click="changeAdd">
				<u-icon name="plus-circle" :color="colortopic()" size="22"></u-icon>
				<text>{{$t('EmployeeAdd')}}</text>
			</view>
		</view>
		<u-popup :show="popshow" :round="10" mode="center" @close="close" @open="open">
			<view :class="i18nLocale=='ug'?'ug-from pop-box' :'pop-box'" :dir="i18nLocale=='ug'?'rtl':''">
				<view class="pop-title">{{$t('Edit')}}</view>
				<view class="input-item">
					<view class="title">
						{{$t('EmployeesName')}}
					</view>
					<u--input v-model="form.name" :placeholder="$t('EmployeesName')"  fontSize="28rpx"
						:adjustPosition='false'></u--input>
				</view>
				<view class="input-item">
					<view class="title">
						{{$t('EmployeesNumer')}}
					</view>
					<u--input v-model="form.mobile" :placeholder="$t('EmployeesNumer')" type="number" fontSize="28rpx"
						:adjustPosition='false'></u--input>
				</view>
				<view class="input-item">
					<view class="title">
						{{$t('ShopType')}}
					</view>
					<view class="" :style="{color:!form.is_locked?'#494FB6':'#cc0000'}">
						{{!form.is_locked? $t('Work'):$t('NOWork')}}
					</view>
					<view class="" style="direction: ltr;">
						<switch :color="colortopic()" @change="change" :checked="!form.is_locked"
							style="transform:scale(0.7);direction: ltr;" />
					</view>
				</view>
				<view class="button-grop">
					<view class="button" :style="{color:colortopic()}" @click="confadd">{{$t('Confirm')}}</view>
					<view class="button" @click="popshow=false">{{$t('TimeCancel')}}</view>
				</view>
			</view>
		</u-popup>

		<u-toast ref="uToast" :message="notmessage"></u-toast>
	</view>
</template>

<script>
	import {
		getemployees,
		postemployees,
		putemployees,
		deleteemployees
	} from '@/api/shop.js';

	export default {
		data() {
			return {
				employeeList: [],
				notmessage: '请仔细填写!',
				popshow: false,
				form: {
					name: '',
					mobile: '',
					is_locked: false,
				},
				tablenumber: '',
				edit_id: '',
			};
		},
		onLoad() {
			this.getEmployees()
		},
		computed: {
			i18nLocale() {
				return this.$i18n.locale;
			},
		},
		methods: {
			getEmployees() {
				let that = this;
				getemployees({}).then((res) => {
					if (res.code == 200) {
						let data = res.body.data;
						that.employeeList = data;
					}
				})
			},
			//添加
			confadd() {
				let that = this;
				that.popshow = false;
				if (!that.form.name && !that.form.mobile) {
					that.$refs.uToast.show({
						top: 0,
						message: this.$t('PleaseFillCarefully'),
						color: '#fff',
						bgColor: '#f0ba53'
					})
					return;
				}

				if (that.edit_id) {
					putemployees({
						id: that.edit_id,
						name: that.form.name,
						mobile: that.form.mobile,
						is_locked: !that.form.is_locked,
					}).then((res) => {
						if (res.code == 200) {
							that.getEmployees();
							that.tablenumber = '';
							that.$refs.uToast.show({
								top: 0,
								message: this.$t('EditSuccessful'),
								color: '#fff',
								bgColor: '#f0ba53'
							})
						} else {
							that.$refs.uToast.show({
								top: 0,
								message: this.$t('EditFailed'),
								color: '#fff',
								bgColor: '#f0ba53'
							})
						}
					})
				} else {
					postemployees({
						name: that.form.name,
						mobile: that.form.mobile,
						is_locked: that.form.is_locked,
					}).then((res) => {
						if (res.code == 200) {
							that.getEmployees();
							that.tablenumber = '';
							that.$refs.uToast.show({
								top: 0,
								message: this.$t('AdditionSuccessful'),
								color: '#fff',
								bgColor: '#f0ba53'
							})
						} else {
							that.$refs.uToast.show({
								top: 0,
								message: this.$t('AdditionFailed'),
								color: '#fff',
								bgColor: '#f0ba53'
							})
						}
					})
				}
			},
			//删除员工
			deletEmployees(id) {
				let that = this;
				deleteemployees({
					id: id
				}).then((res) => {
					if (res.code == 200) {
						that.getEmployees();
						that.$refs.uToast.show({
							top: 0,
							message: this.$t('DeleteSuccessful'),
							color: '#fff',
							bgColor: '#f0ba53'
						})
					} else {
						that.$refs.uToast.show({
							top: 0,
							message: this.$t('DeleteFailed'),
							color: '#fff',
							bgColor: '#f0ba53'
						})
					}
				})
			},
			change(e) {
				this.form.is_locked = e.detail.value;
			},
			open() {},
			close() {
				this.popshow = false
			},
			codeOpen() {},
			codeClose() {
				this.codepop = false;
			},
			//修改员工
			EditEmployee(item) {
				if (item) {
					this.form.name = item.name;
					this.form.mobile = item.mobile;
					this.form.is_locked = item.is_locked;
					this.edit_id = item.id;
				}
				this.popshow = true;
			},
			changeAdd() {
				this.form.name = '';
				this.form.mobile = '';
				this.form.is_locked = false;
				this.edit_id = '';
				this.popshow = true;
			}


		}
	}
</script>

<style lang="scss">
	.main {
		width: 100%;
		height: 100vh;
		position: relative;
	}

	.shop-list {
		width: 100%;
		height: 100%;

		.shop-item {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			background-color: $uni-bg-color-box;
			border-radius: $uni-border-radius-base;
			box-shadow: $uni-bg-shadow;
			margin-top: 30rpx;
			padding: $uni-spacing-padding-sm;

			.left {
				display: flex;
				align-items: center;

				.shop-content {
					height: 200rpx;
					box-sizing: border-box;
					padding: 20rpx;
					display: flex;
					flex-direction: column;
					align-items: center;

					.title {
						color: #000;
						font-size: $uni-font-size-lg ;
						padding: 10rpx;
						display: flex;
						justify-content: center;
						align-items: center;
					}

					.mobile {
						color: #999;
						font-size: $uni-font-size-base ;
						padding: 10rpx;
						display: flex;
						justify-content: center;
						align-items: center;
					}


				}
			}

			.right {
				display: flex;
				align-items: center;
				color: $uni-text-color-grey;
				font-size: $uni-font-size-sm1;

				.button {
					padding: 5rpx 10rpx;
					border-radius: $uni-border-radius-lg;
					margin: 0rpx 10rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;

					text {
						padding: 10rpx 0rpx;
					}
				}

			}
		}

	}

	.footer-button {
		width: 100%;
		height: 100rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-around;
		position: fixed;
		bottom: 0;
		left: 0;

		.button-item {
			width: 80%;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		text {
			padding: 0rpx 20rpx;
		}
	}

	.pop-box {
		width: 600rpx;
		height: 600rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.pop-title {
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20rpx 0rpx;
		}

		.code {
			width: 300rpx;
			height: 300rpx;
			margin-top: 10rpx;

		}

		.input-item {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: $uni-spacing-padding-sm 40rpx;
			box-sizing: border-box;
			margin-top: 10rpx;

			input {
				font-size: $uni-font-size-base;
				// border: 1rpx solid #999;
				text-align: center;
			}

			.title {
				color: $uni-color-title;
				font-size: $uni-font-size-base;
				padding: 0rpx 10rpx;
				// padding: 0rpx 30rpx;'
			}
		}

		.button-grop {
			width: 100%;
			display: flex;
			justify-content: space-around;
			margin-top: 50rpx;

			.button {
				width: 30%;
				border: 1rpx solid #999;
				padding: 10rpx 0rpx;
				border-radius: 20rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}

	.ug-from {
		// direction: rtl;

		/deep/ .uni-input-input {
			// direction: rtl !important;
			// text-align: right;
			text-align: center;
		}

		/deep/ .uni-input-placeholder {
			// text-align: right;
			text-align: center;
		}
	}
</style>